/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.sel1602.csp.Dao.impl;

import edu.fjut.sel1602.csp.Dao.PetOwnerDao;
import edu.fjut.sel1602.csp.entity.PetOwner;
import edu.fjut.sel1602.csp.entity.PetStore;
import edu.fjut.sel1602.csp.util.BaseDao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo
 */
public class PetOwnerDaoOracleImpl implements PetOwnerDao{
    BaseDao bd=new BaseDao();
    @Override
    public int add(PetOwner owner) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("insert into petowner(NAME,PASSWORD,MONEY) values(?,?,?)");
            pst.setString(1, owner.getName());
            pst.setString(2, owner.getPassword());
            pst.setInt(3, owner.getMoney());
            r=pst.executeUpdate();
            BaseDao.closeConnection(con); 
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public List<String> getOwnerName() {
        List<String> list=new ArrayList<String>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select NAME from petowner");
            while(rs.next())
            {
                String name=rs.getString("NAME");
                list.add(name);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

    @Override
    public List<PetOwner> getAllOwner() {
        List<PetOwner> list=new ArrayList<PetOwner>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from petowner");
            while(rs.next())
            {
                PetOwner owner=new PetOwner();
                owner.setId(rs.getInt("ID"));
                owner.setName(rs.getString("NAME"));
                owner.setPassword(rs.getString("PASSWORD"));
                owner.setMoney(rs.getInt("MONEY"));
                list.add(owner);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

   /* @Override
    public int getIdByName(String name) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("select * from petowner where `NAME`=?");
            pst.setString(1, name);
            ResultSet rs =pst.executeQuery();
            if(rs.next()){
                PetOwner po=new PetOwner();
                po.setId(rs.getInt("ID"));
                r=po.getId();
            }
           
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r; 
    }

    @Override
    public int updateByName(int money, String name) {
        try {
            String sql="update petowner set money=? where name=?";
            Object [] objs={money,name};
            int count = bd.execteUpdate(sql, objs);
            return count;
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
	}
    @Override
    public int updateMoneyByName(int money, String name) {
        try {
            String sql="update petowner set money=? where name=?";
            Object [] objs={money,name};
            int count = bd.execteUpdate(sql, objs);
            return count;
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    } */  
}
